'''
Embarcadero C++Builder.
'''

from __future__ import with_statement

import subprocess

from host.helpers import program_files_x86
from os_helpers import LocalCwd
from . import _base, _lang

NAME = "Embarcadero"

SUPPORTED_VERSIONS = [
    ("CodeGear", 5, "2007"),
    ("CodeGear", 6, "2009"),
    ("Embarcadero", 7, "2010"),
    ("Embarcadero", 8, "XE"),
    ("Embarcadero", 9, "XE2"),
    ("Embarcadero", 10, "XE3")
    ]

def check(host, language, description, binary, environment_list):
    """
    Checks a path glob (wildcard) for GCC suite executables, and adds any found
    to environment_list.
    """
    for company, internal_ver, human_ver in SUPPORTED_VERSIONS:
        bds_path = host.path_join(program_files_x86(host),
                                  company,
                                  'RAD Studio',
                                  str(internal_ver) + '.0') 
        exe_path = host.path_join(bds_path,
                                  'bin',
                                  binary)
        if host.path_exists(exe_path):
            environment_list[language.name][description].append(
                EmbarcaderoEnvironment(language, host, bds_path, exe_path, 'C++Builder %s' % human_ver))

def configure_language(host_list, environment_list, language, description, binary):
    """
    Configures the requested language by checking each host in host_list and
    adding detected environments to environment_list.
    """
    for host in host_list:
        if 'nt' not in host.os_name_list:
            continue
        check(host, language, description, binary, environment_list)

def configure(host_list, environment_list):
    configure_language(host_list, environment_list, _lang.Cpp, 'Embarcadero C++Builder', 'bcc32.exe')
    configure_language(host_list, environment_list, _lang.C, 'Embarcadero C++Builder', 'bcc32.exe')

class EmbarcaderoEnvironment(_base.Environment):
    """
    An environment for one of the languages supported by Embarcadero RAD Studio.
    """
    
    has_arguments = True

    def __init__(self, language, host, bds_path, exe_path, version):
        self.language = language
        self.host = host
        self.path = exe_path
        self.version = version
        self.exe_suffix = '.exe'        
        self._bds_path = bds_path

    def compile(self, filename, output):
        """
        Compiles the given source file and sends the result to output.
        """
        file_dirname = self.host.path_dirname(filename)
        file_basename = self.host.path_basename(filename)
        self.host.putenv('BDS', self._bds_path)
        with LocalCwd(file_dirname, self.host):
            # For compatibility with the C# compiler, we list the output flag
            # first.
            cc = self.host.popen([self.path,
                                  file_basename], 
                                 stderr=subprocess.STDOUT)
            stdoutdata = cc.communicate()[0]
            output.write(stdoutdata)

        return cc.returncode == 0
